/* Created by jankoatwarpspeed.com */
// ReSharper disable UnusedParameter

(function($) {
    $.fn.formToWizard = function(options) {
        options = $.extend({
            submitButton: "",
            strict: false,
            width: 800,
            stepsArray: null
        }, options);

        var element = this;

        var oldId = 0;

        var steps = $(element).find("fieldset");
        var count = steps.size();
        var submmitButtonName = "#" + options.submitButton;
        $(submmitButtonName).hide();

        // 2
        $(element).before("<ul id='steps'></ul>");

        steps.each(function(i) {
            $(this).wrap('<div id="step' + i + '"></div>');
            $(this).append("<p id='step" + i + "commands'></p>");

            // 2
            var name = options.stepsArray[i].Caption; // $(this).attr("data-wizard-legend");
            $("#steps").append("<li id='stepDesc" + i + "' data-wizard-id=" + i + " class=" + (options.strict ? "strict" : "") + ">Step " + (i + 1) + "<span>" + name + "</span></li>");
            $("#stepDesc" + i).bind("click", function (e) {
                if (!options.strict) {
                    var newId = parseInt($(this).attr('data-wizard-id'));

                    $("#step" + oldId).hide();
                    $("#step" + newId).animate({
                        opacity: 1
                    }, 2000).show();
                    
                    if (newId + 2 == count)
                        $(submmitButtonName).show();
                    selectStep(newId);
                }
            });

            if (i == 0) {
                createNextButton(i);
                selectStep(i);
            } else if (i == count - 1) {
                $("#step" + i).hide().css('width: 0');
                createPrevButton(i);
            } else {
                $("#step" + i).hide().css('width: 0');
                createPrevButton(i);
                createNextButton(i);
            }
        });

        function createPrevButton(i) {
            var stepName = "step" + i;
            $("#" + stepName + "commands").append('<a href="javascript:void(0)" id="' + stepName + 'Prev">Prev</a>');

            $("#" + stepName + "Prev").bind("click", function(e) {
                $("#" + stepName).hide();
                $("#step" + (i - 1)).animate({
                    opacity: 1
                }, 2000).show();

                $(submmitButtonName).show();
                
                selectStep(i - 1);
            }).button({ icons: { primary: 'ui-icon-arrowthick-1-w' } }).addClass('prev');
        }

        function createNextButton(i) {
            var stepName = "step" + i;
            $("#" + stepName + "commands").append('<a href="javascript:void(0)" id="' + stepName + 'Next">Next</a>');

            $("#" + stepName + "Next").bind("click", function (e) {
                $("#" + stepName).hide();
                $("#step" + (i + 1)).animate({
                    opacity: 1
                }, 2000).show();
                
                if (i + 2 == count)
                    $(submmitButtonName).show();
                
                selectStep(i + 1);
            }).button({ icons: { secondary: 'ui-icon-arrowthick-1-e' } }).addClass('next');
        }

        function selectStep(i) {
            $("#steps li").removeClass("current");
            $("#stepDesc" + i).addClass("current");

            // Assume setup ok!
            $('#' + options.stepsArray[i].FirstField).focus();
           
            oldId = i;
        }

    };
})(jQuery);
// ReSharper restore UnusedParameter
